package br.com.edumobi.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import br.com.edumobi.P;
import br.com.edumobi.R;
import br.com.edumobi.dados.modelos.Aluno;

public class SenhaAlunoFragment extends GenericoFragment {

    private TextView textViewNomeTelaSenhaAluno;
    private EditText editTextSenhaAtualSenhaAluno;
    private EditText editTextNovaSenhaSenhaAluno;
    private EditText editTextRepitaSenhaSenhaAluno;
    private Button buttonAlterarSenhaAluno;
    private ProgressBar progressBarSenhaAluno;

    private AcoesSenhaAlunoFragment activity;
    private SenhaAlunoTask task;

    private Aluno aluno;

    private View.OnClickListener clickButtonAlterarSenhaAluno = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String senhaAtual =  editTextSenhaAtualSenhaAluno.getText().toString();
            String novaSenha = editTextNovaSenhaSenhaAluno.getText().toString();
            String confirmacaoNovaSenha = editTextRepitaSenhaSenhaAluno.getText().toString();

            if (senhaAtual.trim().equals(P.STRING_EMPITY)) {
                activity.onSpeakAndMakeText(R.string.obrigatoriedade_senha_atual);
            } else if (!senhaAtual.equals(aluno.senha)) {
                activity.onSpeakAndMakeText(R.string.excecao_senha_atual_invalida);
            } else if (novaSenha.trim().equals(P.STRING_EMPITY)) {
                activity.onSpeakAndMakeText(R.string.obrigatoriedade_nova_senha);
            } else if (confirmacaoNovaSenha.trim().equals(P.STRING_EMPITY)) {
                activity.onSpeakAndMakeText(R.string.obrigatoriedade_confirmacao_nova_senha);
            } else if (!novaSenha.equals(confirmacaoNovaSenha)) {
                activity.onSpeakAndMakeText(R.string.excecao_nova_senha_confirmacao_nao_conferem);
            } else {
                if (activity.isConnected()) {
                    aluno.senha = novaSenha;

                    task = new SenhaAlunoTask();
                    task.execute();
                } else {
                    activity.onSpeakAndMakeText(R.string.excecao_sem_conexao_internet);
                }
            }
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        activity = (AcoesSenhaAlunoFragment) getActivity();

        aluno = activity.getAluno();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_senha_aluno, container, false);

        textViewNomeTelaSenhaAluno = (TextView) view.findViewById(R.id.textViewNomeTelaSenhaAluno);
        editTextSenhaAtualSenhaAluno = (EditText) view.findViewById(R.id.editTextSenhaAtualSenhaAluno);
        editTextNovaSenhaSenhaAluno = (EditText) view.findViewById(R.id.editTextNovaSenhaSenhaAluno);
        editTextRepitaSenhaSenhaAluno = (EditText) view.findViewById(R.id.editTextRepitaSenhaSenhaAluno);
        buttonAlterarSenhaAluno = (Button) view.findViewById(R.id.buttonAlterarSenhaAluno);
        progressBarSenhaAluno = (ProgressBar) view.findViewById(R.id.progressBarSenha);

        buttonAlterarSenhaAluno.setOnClickListener(clickButtonAlterarSenhaAluno);

        if (task != null && task.getStatus().equals(AsyncTask.Status.RUNNING)) {
            hideComponents();
            progressBarSenhaAluno.setVisibility(View.VISIBLE);
        }

        return view;
    }

    @Override
    public void showComponents() {
        textViewNomeTelaSenhaAluno.setVisibility(View.VISIBLE);
        editTextSenhaAtualSenhaAluno.setVisibility(View.VISIBLE);
        editTextNovaSenhaSenhaAluno.setVisibility(View.VISIBLE);
        editTextRepitaSenhaSenhaAluno.setVisibility(View.VISIBLE);
        buttonAlterarSenhaAluno.setVisibility(View.VISIBLE);
        progressBarSenhaAluno.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideComponents() {
        textViewNomeTelaSenhaAluno.setVisibility(View.GONE);
        editTextSenhaAtualSenhaAluno.setVisibility(View.GONE);
        editTextNovaSenhaSenhaAluno.setVisibility(View.GONE);
        editTextRepitaSenhaSenhaAluno.setVisibility(View.GONE);
        buttonAlterarSenhaAluno.setVisibility(View.GONE);
        progressBarSenhaAluno.setVisibility(View.GONE);
    }

    private class SenhaAlunoTask extends AsyncTask<Void, Void, Boolean>{

        @Override
        protected void onPreExecute() {
            hideComponents();
            progressBarSenhaAluno.setVisibility(View.VISIBLE);
            activity.onSpeak(R.string.aguarde);
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            return activity.onSalvarDadosAluno(aluno);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            showComponents();
            progressBarSenhaAluno.setVisibility(View.GONE);

            if (aBoolean) {
                activity.onSpeakAndMakeText(R.string.senha_alterada_sucesso);
            } else {
                activity.onSpeakAndMakeText(R.string.excecao_autenticacao_mausucedida);
            }
        }
    }

    public interface AcoesSenhaAlunoFragment extends GenericoFragment.AcoesGenericoFragment{
        Aluno getAluno();
        Boolean onSalvarDadosAluno(Aluno aluno);
    }
}
